const util = require('../../utils/util.js')
import * as utils from '../../utils/util.js'
import request from '../../utils/http.js'
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin:false,
    babyList:[],
    currentIndex:0,
    changeBabyData:{},
    ageStr:'',
    planList:[],
    week: '',
    toView:''
  },
  openAddBaby(){
    wx.navigateTo({
      url: '/pages/addBaby/addBaby',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let that = this;
    //获取token数据
      that.setData({
        token: wx.getStorageSync('usertoken'),
        currentIndex: wx.getStorageSync("currentBabyIndex")
      })
  },
  onShow() {
    this.setData({
      currentIndex: wx.getStorageSync("currentBabyIndex")
    })
    let isLogin = wx.getStorageSync('isLogin') || getApp().globalData.isLogin;
    if (isLogin) {
      this.getBabyList()//获取宝宝列表
    }
  },
  //查看baby所有接种计划
  goViewAllPlan(e){
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    if (this.data.planList.length > 0 ){
      let planId = this.data.planList[0].id
      wx.navigateTo({
        url: '/pages/accination-allPlan/accination-allPlan?babyId=' + id + "&name=" + name + "&planId=" + planId
      });
    }else{
      wx.navigateTo({
        url: '/pages/accination-allPlan/accination-allPlan?babyId=' + id + "&name=" + name 
      });
    }
    
  },
  changeBabyHeader(e){
    let index = e.target.dataset.idx;
    wx.setStorageSync('currentBabyIndex', index)
    let currentBaby = this.data.babyList[index]
    this.setData({
      currentIndex: index,
      changeBabyData: currentBaby,
      ageStr: utils.getBirthSlot(currentBaby.birthday.split(" ")[0])
    })
    this.getBabyVaccinationPlan(currentBaby.id) //获取宝宝接种计划
  },
  //获取宝宝列表
  getBabyList() {
    utils.HttpRequst(true, "/baby-cloud-base/mobileBabyInfo/listAll","post",{},res=>{
      if (res.data.code == 200 && res.data.data.length>0) {
        res.data.data.forEach((item,index)=>{
          item.posId = 'idx-' + index
        })
        let index = this.data.currentIndex
        let currentBaby = res.data.data[index]
        console.log(res.data.data)
          this.setData({
            toView: res.data.data[index].posId,
            babyList: res.data.data,
            changeBabyData: currentBaby != undefined ? currentBaby:{},
            ageStr: currentBaby !=undefined ? utils.getBirthSlot(currentBaby.birthday.split(" ")[0]) : '0岁0月'
          })
          // //默认请求一个宝宝的接种计划   参数宝宝id
        if(this.data.babyList.length > 0){
          console.log(this.data.changeBabyData.id)
          this.getBabyVaccinationPlan(this.data.changeBabyData.id)
        }
      }
    })
  },
  //获取宝宝接种计划
  getBabyVaccinationPlan(babyId) {
    utils.HttpRequst(false, '/baby-cloud-base/mobileVaccinePlan/getNextPlan', 'post', { babyId},res=>{
      if (res.data.code == 200) {
        var data = res.data.data
        data.forEach(item => {
          item.planDate = item.planTime.split(" ")[0];
          var currentDate = new Date().getTime();
          var newPlanDate = new Date(item.planTime.replace(/-/g,"/")).getTime();
          item.timeCha = parseInt((currentDate - newPlanDate) / (1000 * 60 * 60 * 24));
          let d = item.differenceDays;
          item.planDay = d
          let arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
          item.week = arr[new Date(item.planDate).getDay()];
        });
        this.setData({
          planList: data,
        });
      }
    })
  },
  // 进入接种详情页
  planDetail: function (e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    let babyId = e.currentTarget.dataset.babyid
    console.log(id)
    wx.navigateTo({
      url: '/pages/accination-planDetail/accination-planDetail?id=' + id + "&babyId=" + babyId
    });
  },

  // 进入自定义疫苗页
  openCustomVaccin: function () {
    wx.navigateTo({
      url: '/pages/customVaccine/customVaccine?babyId=' + this.data.changeBabyData.id
    });
  }
})